package com.snowRemovingUser.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowRemovingUser.domain.TCustomer;
import com.snowRemovingUser.service.ITCustomerService;
import com.snowRemovingUser.util.Md5Utils;
import com.snowRemovingUser.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 除雪防滑系统的客户管理Controller
 *
 * @author liguang
 * @date 2023-03-31
 */
@RestController
@RequestMapping("/snowRemoving/customer")
public class TCustomerController extends BaseController
{
    @Autowired
    private ITCustomerService tCustomerService;

    /**
     * 查询除雪防滑系统的客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCustomer tCustomer)
    {
        startPage();
        List<TCustomer> list = tCustomerService.selectTCustomerList(tCustomer);
        return getDataTable(list);
    }

    /**
     * 导出除雪防滑系统的客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:customer:export')")
    @Log(title = "除雪防滑系统的客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCustomer tCustomer)
    {
        List<TCustomer> list = tCustomerService.selectTCustomerList(tCustomer);
        ExcelUtil<TCustomer> util = new ExcelUtil<TCustomer>(TCustomer.class);
        util.exportExcel(response, list, "除雪防滑系统的客户管理数据");
    }

    /**
     * 获取除雪防滑系统的客户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tCustomerService.selectTCustomerById(id));
    }

    /**
     * 新增除雪防滑系统的客户管理
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:customer:add')")
    @Log(title = "除雪防滑系统的客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCustomer tCustomer)
    {
        tCustomer.setCreateBy(getUsername());
        String md5Pwd = Md5Utils.encrypByMd5(tCustomer.getPassword());
        String salt = StrUtils.getComplexRandomString(32);
        String SaltMd5 = Md5Utils.encrypByMd5(md5Pwd + salt);
        tCustomer.setSalt(salt);
        tCustomer.setPassword(SaltMd5);
        return toAjax(tCustomerService.insertTCustomer(tCustomer));
    }

    /**
     * 修改除雪防滑系统的客户管理
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:customer:edit')")
    @Log(title = "除雪防滑系统的客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCustomer tCustomer)
    {

        tCustomer.setUpdateBy(getUsername());
        return toAjax(tCustomerService.updateTCustomer(tCustomer));
    }

    /**
     * 删除除雪防滑系统的客户管理
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:customer:remove')")
    @Log(title = "除雪防滑系统的客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tCustomerService.deleteTCustomerByIds(ids));
    }



    /**
     * 重置密码除雪防滑系统的客户管理
     */
    @Log(title = "除雪防滑系统的客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/ResetPassword")
    public AjaxResult reset(@RequestBody TCustomer tCustomer)
    {
        String md5Pwd = Md5Utils.encrypByMd5("123456");
        String salt = StrUtils.getComplexRandomString(32);
        String SaltMd5 = Md5Utils.encrypByMd5(md5Pwd + salt);
        tCustomer.setSalt(salt);
        tCustomer.setPassword(SaltMd5);
        return toAjax(tCustomerService.updateTCustomer(tCustomer));
    }





}
